package uoc.biblioteca.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;

public class SendMail {

	public String urlProperties;
	
	public void sendMail(String title,String text,String toSend){
		
		try{
			
			 Properties prop = new Properties();
	    	 
	    	 File f=(new File((urlProperties+"/mail.properties")));
 	    	 FileInputStream archivo = new FileInputStream(f);
 	    	 prop.load(archivo);   	// Cargo propiedades
 	    	
 	    	 // Lectura de propiedad
 	    	String host = prop.getProperty("host");
 	    	String nameUser = prop.getProperty("nameUser");
 	    	String password = prop.getProperty("password");
 	    	String mailFrom = prop.getProperty("mailFrom"); 
 	    	String portMail = prop.getProperty("portMail"); 
			
			Email email = new SimpleEmail();
			email.setHostName(host);
			email.setSmtpPort(new Integer(portMail));
			email.setAuthenticator(new DefaultAuthenticator(nameUser, password));
			email.setTLS(true);
			email.setSSL(true);
			email.setFrom(mailFrom);
			email.setSubject(title);
			email.setMsg(text);
			email.addTo(toSend);
			email.send();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getUrlProperties() {
		return urlProperties;
	}

	public void setUrlProperties(String urlProperties) {
		this.urlProperties = urlProperties;
	}
	
}
